﻿

using DualPlaneFR4Paster.Common.Services;
using Prism.Commands;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;

namespace DualPlaneFR4Paster.ViewModels.Dialogs
{
    public class ParameterDialogViewModel : DialogViewModel
    {
        #region 变量
        string partNum = string.Empty;
        ICameraService camFloor1Up, camFloor1Down, camFloor2Up, camFloor2Down;
        #endregion
        #region 属性
        private bool isSimulate;
        public bool IsSimulate
        {
            get { return isSimulate; }
            set { SetProperty(ref isSimulate, value); }
        }
        private string xYGapLimit;
        public string XYGapLimit
        {
            get { return xYGapLimit; }
            set { SetProperty(ref xYGapLimit, value); }
        }
        private string rGapLimit;
        public string RGapLimit
        {
            get { return rGapLimit; }
            set { SetProperty(ref rGapLimit, value); }
        }
        private string topFilmSuckTime;
        public string TopFilmSuckTime
        {
            get { return topFilmSuckTime; }
            set { SetProperty(ref topFilmSuckTime, value); }
        }
        private string topFilmReleaseTime;
        public string TopFilmReleaseTime
        {
            get { return topFilmReleaseTime; }
            set { SetProperty(ref topFilmReleaseTime, value); }
        }
        private string bottomFilmSuckTime;
        public string BottomFilmSuckTime
        {
            get { return bottomFilmSuckTime; }
            set { SetProperty(ref bottomFilmSuckTime, value); }
        }
        private string bottomFilmReleaseTime;
        public string BottomFilmReleaseTime
        {
            get { return bottomFilmReleaseTime; }
            set { SetProperty(ref bottomFilmReleaseTime, value); }
        }
        private string topDownCameraExposureTime;
        public string TopDownCameraExposureTime
        {
            get { return topDownCameraExposureTime; }
            set { SetProperty(ref topDownCameraExposureTime, value); }
        }
        private string topUpCameraExposureTime;
        public string TopUpCameraExposureTime
        {
            get { return topUpCameraExposureTime; }
            set { SetProperty(ref topUpCameraExposureTime, value); }
        }
        private string bottomDownCameraExposureTime;
        public string BottomDownCameraExposureTime
        {
            get { return bottomDownCameraExposureTime; }
            set { SetProperty(ref bottomDownCameraExposureTime, value); }
        }
        private string bottomUpCameraExposureTime;
        public string BottomUpCameraExposureTime
        {
            get { return bottomUpCameraExposureTime; }
            set { SetProperty(ref bottomUpCameraExposureTime, value); }
        }
        private string railDelayTime;
        public string RailDelayTime
        {
            get { return railDelayTime; }
            set { SetProperty(ref railDelayTime, value); }
        }
        private string railPos;
        public string RailPos
        {
            get { return railPos; }
            set { SetProperty(ref railPos, value); }
        }
        private string capacityPath;
        public string CapacityPath
        {
            get { return capacityPath; }
            set { SetProperty(ref capacityPath, value); }
        }
        private bool railDir;
        public bool RailDir
        {
            get { return railDir; }
            set { SetProperty(ref railDir, value); }
        }
        #endregion
        #region 命令
        private DelegateCommand<object> simulateSwitchCommand;
        public DelegateCommand<object> SimulateSwitchCommand =>
            simulateSwitchCommand ?? (simulateSwitchCommand = new DelegateCommand<object>(ExecuteSimulateSwitchCommand));
        private DelegateCommand<object> textBoxLostFocusCommand;
        public DelegateCommand<object> TextBoxLostFocusCommand =>
            textBoxLostFocusCommand ?? (textBoxLostFocusCommand = new DelegateCommand<object>(ExecuteTextBoxLostFocusCommand));
        private DelegateCommand<object> openDirectoryCommand;
        public DelegateCommand<object> OpenDirectoryCommand =>
            openDirectoryCommand ?? (openDirectoryCommand = new DelegateCommand<object>(ExecuteOpenDirectoryCommand));
        private DelegateCommand railDirCommand;
        public DelegateCommand RailDirCommand =>
            railDirCommand ?? (railDirCommand = new DelegateCommand(ExecuteRailDirCommand));

        void ExecuteRailDirCommand()
        {
            using var db = new DTContext(partNum);
            var v1 = db.MParams.FirstOrDefault(p => p.Name == "RailDir");
            if (v1 != null)
            {
                v1.Value = RailDir.ToString();
                v1.Type = typeof(bool).Name;
                db.SaveChanges();
            }
        }
        void ExecuteOpenDirectoryCommand(object obj)
        {
            using var fbd = new FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = fbd.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
            {
                switch (obj.ToString())
                {
                    case "CapacityPath":
                        {
                            CapacityPath = fbd.SelectedPath;
                            using var sysdb = new SysContext();
                            var v2 = sysdb.MSystemParams.FirstOrDefault(p => p.Name == "CapacityPath");
                            if (v2 != null)
                            {
                                v2.Value = CapacityPath;
                                sysdb.SaveChanges();
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        void ExecuteTextBoxLostFocusCommand(object obj)
        {
            using var db = new DTContext(partNum);
            switch ((string)obj)
            {
                case "0":
                    {
                        if (double.TryParse(XYGapLimit, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "XYGapLimit");
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();
                            }
                        }
                    }
                    break;
                case "1":
                    {
                        if (double.TryParse(RGapLimit, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "RGapLimit");
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();
                            }
                        }
                    }
                    break;
                case "TopFilmSuckTime":
                    {
                        if (double.TryParse(TopFilmSuckTime, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "FilmSuckTime" && p.Floor == 1);
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();
                            }
                        }
                    }
                    break;
                case "TopFilmReleaseTime":
                    {
                        if (double.TryParse(TopFilmReleaseTime, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "FilmReleaseTime" && p.Floor == 1);
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();
                            }
                        }
                    }
                    break;
                case "BottomFilmSuckTime":
                    {
                        if (double.TryParse(BottomFilmSuckTime, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "FilmSuckTime" && p.Floor == 2);
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();
                            }
                        }
                    }
                    break;
                case "BottomFilmReleaseTime":
                    {
                        if (double.TryParse(BottomFilmReleaseTime, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "FilmReleaseTime" && p.Floor == 2);
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();
                            }
                        }
                    }
                    break;
                case "TopDownCameraExposureTime":
                    {
                        if (float.TryParse(TopDownCameraExposureTime, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "DownCameraExposureTime" && p.Floor == 1);
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();
                            }
                            camFloor1Down.SetExposureTime(v0);
                        }
                    }
                    break;
                case "TopUpCameraExposureTime":
                    {
                        if (float.TryParse(TopUpCameraExposureTime, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "UpCameraExposureTime" && p.Floor == 1);
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();
                            }
                            camFloor1Up.SetExposureTime(v0);
                        }
                    }
                    break;
                case "BottomDownCameraExposureTime":
                    {
                        if (float.TryParse(BottomDownCameraExposureTime, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "DownCameraExposureTime" && p.Floor == 2);
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();                                
                            }
                            camFloor2Down.SetExposureTime(v0);
                        }
                    }
                    break;
                case "BottomUpCameraExposureTime":
                    {
                        if (float.TryParse(BottomUpCameraExposureTime, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "UpCameraExposureTime" && p.Floor == 2);
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();
                            }
                            camFloor2Up.SetExposureTime(v0);
                        }
                    }
                    break;
                case "RailDelayTime":
                    {
                        if (double.TryParse(RailDelayTime, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "RailDelayTime");
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();
                            }
                        }
                    }
                    break;
                case "RailPos":
                    {
                        if (float.TryParse(RailPos, out var v0))
                        {
                            var v1 = db.MParams.FirstOrDefault(p => p.Name == "RailPos");
                            if (v1 != null)
                            {
                                v1.Value = v0.ToString();
                                db.SaveChanges();
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        void ExecuteSimulateSwitchCommand(object obj)
        {
            //Debug.WriteLine($"{obj};{IsSimulate}");
            using (var db = new DTContext(partNum))
            {
                var v1 = db.MParams.FirstOrDefault(p => p.Name == "IsSimulate");
                if (v1 != null)
                {
                    v1.Value = (bool)obj ? "1" : "0";
                    db.SaveChanges();
                }
            }
        }
        #endregion
        #region 导航
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            partNum = parameters.GetValue<string>("PartNum");
            using var db = new DTContext(partNum);
            var v1 = db.MParams.FirstOrDefault(p => p.Name == "IsSimulate");
            if (v1 != null)
            {
                IsSimulate = v1.Value == "1";
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "XYGapLimit");
            if (v1 != null)
            {
                XYGapLimit = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "RGapLimit");
            if (v1 != null)
            {
                RGapLimit = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "FilmSuckTime" && p.Floor == 1);
            if (v1 != null)
            {
                TopFilmSuckTime = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "FilmReleaseTime" && p.Floor == 1);
            if (v1 != null)
            {
                TopFilmReleaseTime = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "FilmSuckTime" && p.Floor == 2);
            if (v1 != null)
            {
                BottomFilmSuckTime = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "FilmReleaseTime" && p.Floor == 2);
            if (v1 != null)
            {
                BottomFilmReleaseTime = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "UpCameraExposureTime" && p.Floor == 1);
            if (v1 != null)
            {
                TopUpCameraExposureTime = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "DownCameraExposureTime" && p.Floor == 1);
            if (v1 != null)
            {
                TopDownCameraExposureTime = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "UpCameraExposureTime" && p.Floor == 2);
            if (v1 != null)
            {
                BottomUpCameraExposureTime = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "DownCameraExposureTime" && p.Floor == 2);
            if (v1 != null)
            {
                BottomDownCameraExposureTime = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "RailDelayTime");
            if (v1 != null)
            {
                RailDelayTime = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "RailPos");
            if (v1 != null)
            {
                RailPos = v1.Value;
            }
            v1 = db.MParams.FirstOrDefault(p => p.Name == "RailDir");
            if (v1 != null)
            {
                if (bool.TryParse(v1.Value,out var result))
                {
                    RailDir = result;
                }
                else
                {
                    RailDir = false;
                }
            }
            using var sysdb = new SysContext();
            var v2 = sysdb.MSystemParams.FirstOrDefault(p => p.Name == "CapacityPath");
            if (v2 != null)
            {
                CapacityPath = v2.Value;
            }

        }
        #endregion
        public ParameterDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            Title = "参数设置";
            XYGapLimit = 0.ToString();
            RGapLimit = 0.ToString();
            camFloor1Up = containerProvider.Resolve<ICameraService>("Floor1Up");
            camFloor1Down = containerProvider.Resolve<ICameraService>("Floor1Down");
            camFloor2Up = containerProvider.Resolve<ICameraService>("Floor2Up");
            camFloor2Down = containerProvider.Resolve<ICameraService>("Floor2Down");
        }
    }
}
